package cn.tedu.medicalmanagementsystem.user.controller;

import cn.tedu.medicalmanagementsystem.base.response.JsonResult;
import cn.tedu.medicalmanagementsystem.base.response.StatusCode;
import cn.tedu.medicalmanagementsystem.user.pojo.dto.UserFavoriteAddParam;
import cn.tedu.medicalmanagementsystem.user.pojo.vo.UserEscortVO;
import cn.tedu.medicalmanagementsystem.user.pojo.vo.UserFavoriteVO;
import cn.tedu.medicalmanagementsystem.user.pojo.vo.UserVO;
import cn.tedu.medicalmanagementsystem.user.service.UserEscortService;
import cn.tedu.medicalmanagementsystem.user.service.UserFavoriteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user/favorites")
@Api(tags = "用户收藏管理")
public class UserFavoriteController {

    @Autowired
    private UserFavoriteService userFavoriteService;

    // 添加收藏
    @PostMapping("/add")
    @ApiOperation("添加收藏")
    public JsonResult add(@RequestBody UserFavoriteAddParam param, HttpSession session) {
        Long userId = getCurrentUserId(session);
        if (userId == null) {
            return JsonResult.fail(StatusCode.NOT_LOGIN);
        }
        return userFavoriteService.addFavorite(param, userId);
    }

    // 取消收藏
    @DeleteMapping("/cancel/{escortId}")
    @ApiOperation("取消收藏")
    public JsonResult remove(@PathVariable Long escortId, HttpSession session) {
        Long userId = getCurrentUserId(session);
        if (userId == null) {
            return JsonResult.fail(StatusCode.NOT_LOGIN);
        }
        return userFavoriteService.removeFavorite(escortId, userId);
    }

    // 获取收藏列表
    @GetMapping("/list")
    @ApiOperation("获取收藏列表")
    public JsonResult<List<UserFavoriteVO>> list(HttpSession session) {
        Long userId = getCurrentUserId(session);
        if (userId == null) {
            return JsonResult.fail(StatusCode.NOT_LOGIN);
        }
        return userFavoriteService.listFavorites(userId);
    }

    // 检查是否收藏
    @GetMapping("/check/{escortId}")
    @ApiOperation("检查是否收藏")
    public JsonResult<Boolean> check(@PathVariable Long escortId, HttpSession session) {
        Long userId = getCurrentUserId(session);
        if (userId == null) {
            return JsonResult.fail(StatusCode.NOT_LOGIN);
        }
        return userFavoriteService.checkFavorite(escortId, userId);
    }

/*    private Long getCurrentUserId(HttpSession session) {
        UserVO user = (UserVO) session.getAttribute("user");
        return user != null ? user.getUserId() : null;
    }*/

    @Autowired
    private UserEscortService userEscortService;

    // 搜索陪诊员
    @GetMapping("/search-escort")
    @ApiOperation("搜索陪诊员")
    public JsonResult<List<UserEscortVO>> searchEscorts(
            @RequestParam String keyword,
            @RequestParam Integer page,
            @RequestParam Integer size) {
        List<UserEscortVO> escorts = userEscortService.searchEscorts(keyword, page, size);
        return JsonResult.ok(escorts);
    }

    private Long getCurrentUserId(HttpSession session) {
        Object userObj = session.getAttribute("user");
        if (userObj == null) {
            log.warn("Session 中没有找到用户信息, session id: {}", session.getId());
            return null;
        }
        UserVO user = (UserVO) userObj;
        log.info("session中找到: {}", user);
        return user.getId();
    }
}

